# Maintainer: George Rawlinson <grawlinson@archlinux.org>
# Contributor: Felix Yan <felixonmars@archlinux.org>
# Contributor: Mario Finelli <mario at finel dot li>
# Contributor: Artem Vorotnikov <artem at vorotnikov dot me>

_gemname=ffi
pkgname=ruby-$_gemname
pkgver=1.16.3
pkgrel=1
pkgdesc='Ruby FFI'
arch=('loong64' 'x86_64')
url='https://github.com/ffi/ffi'
license=('BSD')
depends=('ruby')
makedepends=('ruby-rdoc')
options=('!emptydirs')
source=("https://rubygems.org/downloads/$_gemname-$pkgver.gem")
noextract=("$_gemname-$pkgver.gem")
sha512sums=('b3d823a03055412a85ae3dbc10c3b50615614f0b66830e144ca47610b1f93f588ff693a95d364b4f686968b79bba91f9f9fa60b932479c6bf9ceb10e15575b98')
b2sums=('887c28320af9caa7fa2d38e2be7775413c0fc4385dab9763279e41bf993b149f8cef6e117ac4f1eeccf9afabb41ee990507cb901483c143ba4e872a165b7fb77')

package() {
  local _gemdir="$(ruby -e'puts Gem.default_dir')"

  gem install \
    --verbose \
    --ignore-dependencies \
    --no-document \
    --no-user-install \
    --install-dir "$pkgdir/$_gemdir" \
    --bindir "$pkgdir/usr/bin" \
    "$_gemname-$pkgver.gem"

  # delete unnecessary files & folders
  cd "$pkgdir/$_gemdir"
  find . -type f -name 'gem_make.out' -delete -o -name 'mkmf.log' -delete
  rm -vrf cache
  cd "gems/$_gemname-$pkgver"
  rm -vrf Gemfile Rakefile "$_gemname.gemspec" ext rakelib
  find lib/ffi/platform/* -prune -not -name "$CARCH-linux" -exec rm -rf {} +

  # move documentation
  install -vd "$pkgdir/usr/share/doc/$pkgname"
  mv -v samples CHANGELOG.md README.md "$pkgdir/usr/share/doc/$pkgname"

  # move licenses
  install -vd "$pkgdir/usr/share/licenses/$pkgname"
  mv -v LICENSE* COPYING "$pkgdir/usr/share/licenses/$pkgname"

  # generate Ruby documentation
  rdoc \
    --format ri \
    --output "$pkgdir$_gemdir/doc/$_gemname-$pkgver/ri" \
    ./lib
  # delete unnecessary artifacts
  rm -rf "$pkgdir$_gemdir/doc/$_gemname-$pkgver/ri/"{lib,created.rid}
}
